This is my first article. In this article I have shown you how to load a GridView Using Stored Procedure.
Stored Procedure:-
create procedure usp_GetEmployeeDetails
as
begin
select eid,ename,eaddress,ecity,estate from employee
end
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>A Sample Program Illustrating The Data Retrieval Using Stored Procedure</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Data Source=INNOVA14;uid=sa;pwd=innova;Initial Catalog=Employee");
protected void Page_Load(object sender, EventArgs e)
{
loadmyGridUsingStoredProcedure();
}
public void loadmyGridUsingStoredProcedure()
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("usp_GetEmployeeDetails", conn);
SqlCommand comm = new SqlCommand();
comm.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
}
Leave Comment
2 Comments